package com.lagou.module2.task1;
import java.util.Arrays;
/**
 * 绘制棋盘及落子的class
 */

public class ChessBoard {
    //成员对象
    private int[] arr = new int[2];
    static String[][] arr1 = new String[17][17];
    private String[] arr2 = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
    private String[] arr3 = {"a", "b", "c", "d", "e", "f"};
    private boolean repeat;
    private int cntBlack = 4;
    private int cntWhite = 4;

    //构造方法
    public ChessBoard() {
    }
    //绘制棋盘方法
    public void getBoard() {
        arr1[0][0] = " ";
        for (int i=1;i<11; i++){
            arr1[0][i] = arr2[i-1];
            arr1[i][0] = arr2[i-1];
        }
        for (int i=11; i<arr1.length; i++){
            arr1[0][i] = arr3[i-11];
            arr1[i][0] = arr3[i-11];
        }
        for (int i=1; i<arr1.length; i++){
            for (int j=1; j<arr1.length; j++){
                arr1[i][j] = "+";
            }
        }
        for (int i=0; i<arr1.length; i++){
            for (int j=0; j<arr1.length; j++){
                System.out.print(arr1[i][j]+"\t");
            }
            System.out.println();
        }
    }
    //判断该坐标是否有棋子
    public boolean isRepeat(int[] arr){
        if (arr1[arr[1]+1][arr[0]+1] != "+"){
            System.out.println("无法落在已下的棋子上，请重新再下");
            repeat = true;
        }else {repeat = false;}
        return repeat;
    }
    //将输入的a b形式坐标转换为长度为2的一维数组形式
    public int[] getCoordinates(String[] str){
        for (int i=0; i<str.length; i++) {
            switch (str[i]) {
                case "0": arr[i] = 0; break;
                case "1": arr[i] = 1; break;
                case "2": arr[i] = 2; break;
                case "3": arr[i] = 3; break;
                case "4": arr[i] = 4; break;
                case "5": arr[i] = 5; break;
                case "6": arr[i] = 6; break;
                case "7": arr[i] = 7; break;
                case "8": arr[i] = 8; break;
                case "9": arr[i] = 9; break;
                case "a": arr[i] = 10; break;
                case "b": arr[i] = 11; break;
                case "c": arr[i] = 12; break;
                case "d": arr[i] = 13; break;
                case "e": arr[i] = 14; break;
                case "f": arr[i] = 15; break;
                default: System.out.println("坐标输入有误");
            }
        }
        return arr;
    }
    //黑棋落子
    public String[][] blackPlay(int[] arr) {
        //将一维数组对应到棋盘上并改变数值
        arr1[arr[1]+1][arr[0]+1] = "&";
            for (int i=0; i<arr1.length; i++) {
                for (int j = 0; j < arr1.length; j++) {
                    System.out.print(arr1[i][j] + "\t");
            }
            System.out.println(); }
        return arr1;
    }
    //白棋落子
    public String[][] whitePlay(int[] arr) {
            //将一维数组对应到棋盘上并改变数值
            arr1[arr[1]+1][arr[0]+1] = "#";
            for (int i=0; i<arr1.length; i++) {
                for (int j = 0; j < arr1.length; j++) {
                    System.out.print(arr1[i][j] + "\t");
                }
                System.out.println(); }
        return arr1;
    }
    //判断黑棋的胜利条件：用八个for循环判断八个方向是否有黑色棋子连成一线
    public boolean ifBlack(int[] arr){
        String a1 = arr1[arr[1] + 1][arr[0] + 1];
        String a2 = "";
        for (int i = 1; i < 6; i++) {
            //解决出界的bug
            if (arr[1] + 1 <0||arr[0] + i + 1<0||arr[1] + 1>16||arr[0] + i + 1>16) {
                a2 = "+";
            }else{ a2 = arr1[arr[1] + 1][arr[0] + i + 1];}
            if (a1.equals(a2)) {
                a1 = a2;
                cntBlack--;
                if (cntBlack == 0) {
                    return true;
                }
            }else {cntBlack=4;break;}
        }

        for (int i = 1; i < 6; i++) {
            if (arr[1] + 1 + i <0||arr[0] + 1 + i<0||arr[1] + 1 + i>16||arr[0] + 1 + i>16) {
                a2 = "+";
            }else{ a2 = arr1[arr[1] + i + 1][arr[0] + i + 1];}
            if (a1.equals(a2)) {
                a1 = a2;
                cntBlack--;
                if (cntBlack == 0) {
                    return true;
                }
            }else {cntBlack=4;break;}
        }

        for (int i = 1; i < 6; i++) {
            if (arr[1] + 1 - i <0||arr[0] + 1 + i<0||arr[1] + 1 - i>16||arr[0] + 1 + i>16) {
                a2 = "+";
            }else{ a2 = arr1[arr[1] + 1 - i][arr[0] + i + 1];}
            if (a1.equals(a2)) {
                a1 = a2;
                cntBlack--;
                if (cntBlack == 0) {
                    return true;
                }
            }else {cntBlack=4;break;}
        }

        for (int i = 1; i < 6; i++) {
            if (arr[1] + 1 + i <0||arr[0] + 1<0||arr[1] + 1 + i>16||arr[0] + 1>16) {
                a2 = "+";
            }else{ a2 = arr1[arr[1] + 1 + i][arr[0] + 1];}
            if (a1.equals(a2)) {
                a1 = a2;
                cntBlack--;
                if (cntBlack == 0) {
                    return true;
                }
            }else {cntBlack=4;break;}
        }

        for (int i = 1; i < 6; i++) {
            if (arr[1] + 1 - i <0||arr[0] + 1<0||arr[1] + 1 - i>16||arr[0] + 1>16) {
                a2 = "+";
            }else{ a2 = arr1[arr[1] + 1 - i][arr[0] + 1];}
            if (a1.equals(a2)) {
                a1 = a2;
                cntBlack--;
                if (cntBlack == 0) {
                    return true;
                }
            }else {cntBlack=4;break;}
        }

        for (int i = 1; i < 6; i++) {
            if (arr[1] + 1 <0||arr[0] - i + 1<0||arr[1] + 1 >16||arr[0] - i + 1>16) {
                a2 = "+";
            }else{ a2 = arr1[arr[1] + 1][arr[0] - i + 1];}
            if (a1.equals(a2)) {
                a1 = a2;
                cntBlack--;
                if (cntBlack == 0) {
                    return true;
                }
            }else {cntBlack=4;break;}
        }

        for (int i = 1; i < 6; i++) {
            if (arr[1] + 1 + i <0||arr[0] - i + 1<0||arr[1] + 1 + i>16||arr[0] + 1 - i>16) {
                a2 = "+";
            }else{ a2 = arr1[arr[1] + 1 + i][arr[0] - i + 1];}
            if (a1.equals(a2)) {
                a1 = a2;
                cntBlack--;
                if (cntBlack == 0) {
                    return true;
                }
            }else {cntBlack=4;break;}
        }

        for (int i = 1; i < 6; i++) {
            if (arr[1] + 1 - i <0||arr[0] - i + 1<0||arr[1] + 1 - i>16||arr[0] - i + 1>16) {
                a2 = "+";
            }else{ a2 = arr1[arr[1] + 1 - i][arr[0] - i + 1];}
            if (a1.equals(a2)) {
                a1 = a2;
                cntWhite--;
                if (cntWhite == 0) {
                    return true;
                }
            }else {cntBlack=4;break;}
        }
        return false;
    }

    //判断白棋的胜利条件：用八个for循环判断八个方向是否有白色棋子连成一线(与上一方法一致)
    public boolean ifWhite(int[] arr){
        String a1 = arr1[arr[1] + 1][arr[0] + 1];
        String a2 = "";
        //解决出界的bug
        for (int i = 1; i < 6; i++) {
            if (arr[1] + 1 <0||arr[0] + i + 1<0||arr[1] + 1>16||arr[0] + i + 1>16) {
                a2 = "+";
            }else{ a2 = arr1[arr[1] + 1][arr[0] + i + 1];}
            if (a1.equals(a2)) {
                a1 = a2;
                cntWhite--;
                if (cntWhite == 0) {
                    return true;
                }
            }else {cntWhite=4;break;}
        }

        for (int i = 1; i < 6; i++) {
            if (arr[1] + 1 + i <0||arr[0] + 1 + i<0||arr[1] + 1 + i>16||arr[0] + 1 + i>16) {
                a2 = "+";
            }else{ a2 = arr1[arr[1] + i + 1][arr[0] + i + 1];}
            if (a1.equals(a2)) {
                a1 = a2;
                cntWhite--;
                if (cntWhite == 0) {
                    return true;
                }
            }else {cntWhite=4;break;}
        }

        for (int i = 1; i < 6; i++) {
            if (arr[1] + 1 - i <0||arr[0] + 1 + i<0||arr[1] + 1 - i>16||arr[0] + 1 + i>16) {
                a2 = "+";
            }else{ a2 = arr1[arr[1] + 1 - i][arr[0] + i + 1];}
            if (a1.equals(a2)) {
                a1 = a2;
                cntWhite--;
                if (cntWhite == 0) {
                    return true;
                }
            }else {cntWhite=4;break;}
        }

        for (int i = 1; i < 6; i++) {
            if (arr[1] + 1 + i <0||arr[0] + 1<0||arr[1] + 1 + i>16||arr[0] + 1>16) {
                a2 = "+";
            }else{ a2 = arr1[arr[1] + 1 + i][arr[0] + 1];}
            if (a1.equals(a2)) {
                a1 = a2;
                cntWhite--;
                if (cntWhite == 0) {
                    return true;
                }
            }else {cntWhite=4;break;}
        }

        for (int i = 1; i < 6; i++) {
            if (arr[1] + 1 - i <0||arr[0] + 1<0||arr[1] + 1 - i>16||arr[0] + 1>16) {
                a2 = "+";
            }else{ a2 = arr1[arr[1] + 1 - i][arr[0] + 1];}
            if (a1.equals(a2)) {
                a1 = a2;
                cntWhite--;
                if (cntWhite == 0) {
                    return true;
                }
            }else {cntWhite=4;break;}
        }

        for (int i = 1; i < 6; i++) {
            if (arr[1] + 1 <0||arr[0] - i + 1<0||arr[1] + 1 >16||arr[0] - i + 1>16) {
                a2 = "+";
            }else{ a2 = arr1[arr[1] + 1][arr[0] - i + 1];}
            if (a1.equals(a2)) {
                a1 = a2;
                cntWhite--;
                if (cntWhite == 0) {
                    return true;
                }
            }else {cntWhite=4;break;}
        }

        for (int i = 1; i < 6; i++) {
            if (arr[1] + 1 + i <0||arr[0] - i + 1<0||arr[1] + 1 + i>16||arr[0] + 1 - i>16) {
                a2 = "+";
            }else{ a2 = arr1[arr[1] + 1 + i][arr[0] - i + 1];}
            if (a1.equals(a2)) {
                a1 = a2;
                cntWhite--;
                if (cntWhite == 0) {
                    return true;
                }
            }else {cntWhite=4;break;}
        }

        for (int i = 1; i < 6; i++) {
            if (arr[1] + 1 - i <0||arr[0] - i + 1<0||arr[1] + 1 - i>16||arr[0] - i + 1>16) {
                a2 = "+";
            }else{ a2 = arr1[arr[1] + 1 - i][arr[0] - i + 1];}
            if (a1.equals(a2)) {
                a1 = a2;
                cntWhite--;
                if (cntWhite == 0) {
                    return true;
                }
            }else {cntWhite=4;break;}
        }
        return false;
    }
}
